//-----------------------------------------------------------------------------
// Filename: CallHistoryController.cs
//
// Description: GEt's user's call detail records.
//
// History:
// 19 Feb 2008	Aaron Clauson	Created.
//
// License: 
// Aaron Clauson.
//-----------------------------------------------------------------------------

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using MaverickLite.Security;
using BlueFace.Sys;
using BlueFace.VoIP.Net.SIP;
using log4net;

namespace SIPSwitch.Web
{
	public class CallHistoryController : SecureXmlController
	{
		private new static ILog logger = AppState.GetLogger("sipswitch");

        private static string m_switchDBConnStr = ConfigurationManager.AppSettings["SIPSwitchDBConnStr"];
        private static string m_switchStorageTypeStr = ConfigurationManager.AppSettings["SIPSwitchDBType"];
        private static string m_switchServiceHostname = ConfigurationManager.AppSettings["SIPSwitchServiceHostname"];
		private StorageLayer m_sipSwitchStorageLayer = null;

		public CallHistoryController(XmlElement initParams) : base(initParams)
		{
            m_sipSwitchStorageLayer = new StorageLayer(StorageTypesConverter.GetStorageType(m_switchStorageTypeStr), m_switchDBConnStr);
		}

		protected override void InitialiseRequest(HttpContext context)
		{
			try
			{
				base.InitialiseRequest(context);

                if (this.m_authenticated)
                {
                    string username = this.m_authenticatedCredentials.Username;

                    DataSet cdrSet = m_sipSwitchStorageLayer.GetDataSet("select * from cdr where owner = '" + username + "' order by inserted desc limit 50");

                    string cdrXML = "<cdrs>";

                    if (cdrSet != null)
                    {
                        foreach (DataRow cdrRow in cdrSet.Tables[0].Rows)
                        {
                            SIPCDR sipCDR = new SIPCDR(cdrRow);

                            string dst = (sipCDR.Destination.ToString().Length > 30) ? sipCDR.Destination.ToString().Substring(0, 30) + ".." : sipCDR.Destination.ToString();
                            string from = (sipCDR.From.ToString().Length > 30) ? sipCDR.From.ToString().Substring(0, 30) + ".." : sipCDR.From.ToString();

                            cdrXML +=
                                "<cdr>" +
                                " <dirn>" + sipCDR.CallDirection + "</dirn>" +
                                " <calldate>" + sipCDR.Created.ToString("dd MMM yy HH:mm:ss") + "</calldate>" +
                                " <dst><![CDATA[" + dst + "]]></dst>" +
                                " <from><![CDATA[" + from + "]]></from>" +
                                " <dur>" + sipCDR.GetAnsweredDuration().ToString("0") + "</dur>" +
                                " <respcode>" + sipCDR.AnswerStatus + "</respcode>" +
                                " <resp>" + sipCDR.AnswerReasonPhrase + "</resp>" +
                                "</cdr>";
                        }
                    }

                    cdrXML += "</cdrs>";

                    this.m_pageModel.AddToModel("<page>" + cdrXML + "</page>", true);
                }
                else
                {
                    this.m_view = "unauthenticated";
                }
			}
			catch(Exception excp)
			{
				logger.Error("Exception CallHistoryController. " + excp.Message);
				this.m_view = "unauthenticated";
			}
		}
	}
}
